﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SGRapidForm.Common;
using SGRapidForm.Foundation.Entity;
using System.Collections.Specialized;
using FishWebLib;
using System.Data;

namespace SGRapidForm.Foundation.Component
{
    public class AutoGennoComponet : ComponentBase
    {
        /// <summary>
        /// 新增一条自动编码记录数据
        /// </summary>
        /// <param name="foundationClass">一条自动编码记录的记录信息</param>
        /// <returns>是否新增成功</returns>
        public bool AddAutoGenno(AutoGennoEntity auto)
        {
            return (FishBLLHelper.CallSpExecuteNonQuery("sgrf_foundation_AutoGenno_Add", auto) > 0);
        }

        /// <summary>
        /// 根据Keyid获取一条自动编码记录
        /// </summary>
        /// <param name="keyid">Keyid</param>
        /// <returns>自动编码记录</returns>
        public AutoGennoEntity GetAutoGennoByKeyid(string keyid)
        {
            return FishBLLHelper.CallSpGetDataItem<AutoGennoEntity>("sgrf_foundation_AutoGenno_GetAutoGennoByKeyid", null, keyid);
        }

        /// <summary>
        /// 更新一条记录
        /// </summary>
        /// <param name="UserDetails">一条记录</param>
        /// <returns>是否更新成功</returns>
        public bool UpdateAuto(AutoGennoEntity auto)
        {
            return (FishBLLHelper.CallSpExecuteNonQuery("sgrf_foundation_AutoGenno_Update", auto) > 0);
        }

        /// <summary>
        /// 批量删除编码
        /// </summary>
        /// <param name="keyids">keyids</param>
        /// <returns>批量删除是否成功</returns>
        public bool BulkDeleteAuto(string keyids)
        {
            //Keyid数量
            int count = SqlHelper.GetKeyidsCount(keyids);
            //Keyids转换成Sql语句
            keyids = SqlHelper.ConvertKeyidsToSqlStatement(keyids);

            return (FishBLLHelper.CallSpExecuteNonQuery("sgrf_foundation_AutoGenno_BulkDelete", null, keyids) == count);
        }

        /// <summary>
        /// 进行过滤、排序、分页查询自动编码
        /// </summary>
        /// <param name="startRecord">开始记录索引</param>
        /// <param name="maxRecords">每页记录数</param>
        /// <param name="sortColumns">排序列</param>
        /// <param name="filterConditions">过滤条件SQL</param>
        /// <returns>类别集合</returns>
        public List<AutoGennoEntity> PagingGetAutoGenno(Int32 startRecord, Int32 maxRecords, String sortColumns, String filterConditions)
        {
            VerifySortColumns(sortColumns);
            if (string.IsNullOrWhiteSpace(sortColumns))
            {
                sortColumns = "CreateDate DESC";
            }
            NameValueCollection fieldCollection = SqlHelper.ParseQueryString(filterConditions);
            StringBuilder sql = new StringBuilder();
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "Noclass", DbType.String, " AND Noclass like '%{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "Createname", DbType.String, " AND Createname like '%{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "Createdate", DbType.DateTime, " AND CONVERT(char(10), Createdate,120) = '{0}' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "Modifyname", DbType.String, " AND Modifyname like '%{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "Autogenid", DbType.Boolean, " AND Autogenid = '{0}' "));
            filterConditions = sql.ToString();
            return FishBLLHelper.CallSpGetDataItemList<AutoGennoEntity>("sgrf_foundation_AutoGenno_PagingGetAutoGenno", null, startRecord, maxRecords, sortColumns, filterConditions);
        }

        /// <summary>
        /// 验证排序参数是否包含非法字符，有则抛出异常
        /// </summary>
        /// <param name="sortColumns"></param>
        private void VerifySortColumns(string sortColumns)
        {
            sortColumns = sortColumns.ToLowerInvariant().Replace(" asc", String.Empty).Replace(" desc", String.Empty);
            String[] columnNames = sortColumns.Split(',');

            foreach (String columnName in columnNames)
            {

                switch (columnName.Trim().ToLowerInvariant())
                {
                    case "noclass":
                    case "createname":
                    case "modifyname":
                    case "autogenid":
                    case "createdate":
                    case "":
                        break;
                    default:
                        throw new ArgumentException("SortColumns contains an invalid column name.");
                }
            }
        }

        /// <summary>
        /// 进行过滤，排序、分页查询基础资料类别记录数
        /// </summary>
        /// <param name="filterConditions">过滤条件</param>
        /// <returns>记录数</returns>
        public int GetAutoGennoCount(String filterConditions)
        {
            NameValueCollection fieldCollection = SqlHelper.ParseQueryString(filterConditions);
            StringBuilder sql = new StringBuilder();
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "Noclass", DbType.String, " AND Noclass like '%{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "Createname", DbType.String, " AND Createname like '%{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "Createdate", DbType.DateTime, " AND CONVERT(char(10), Createdate,120) = '{0}' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "Modifyname", DbType.String, " AND Modifyname like '%{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "Autogenid", DbType.Boolean, " AND Autogenid = '{0}' "));
            filterConditions = sql.ToString();
            return Convert.ToInt32(FishBLLHelper.CallSpExecuteScalar("sgrf_foundation_AutoGenno_PagingGetAutoGennoCount", null, filterConditions));
        }

        /// <summary>
        /// 获取指定类别自动编码
        /// </summary>
        /// <param name="classCode">编码类别</param>
        /// <param name="deptCode">部门编码</param>
        /// <returns></returns>
        public string GetNoByNoClass(string classCode, string deptCode)
        {
            return FishBLLHelper.CallSpExecuteScalar("sgrf_foundation_AutoGenno_GetNoByNoClass", null, classCode, deptCode).ToString();
        }
    }
}
